package com.thti.mecool.client.activity;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.thti.mecool.client.adapter.AddPlanAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.send.plan.PlanSend;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.model.biz.ProjectServerBizImpl;
import com.thti.mecool.client.model.po.ProjectServer;
import com.thti.mecool.client.protocol.MessageInfo;
import com.thti.mecool.client.protocol.system.ProjectResultProtocol;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class MakePlanSelectProjectActivity extends BaseActivity {

	private ListView addPlanListView;
	private AutoCompleteTextView autoCompleteTextView;
	public static Handler handler;
	private MessageInfo messageInfo;
	private ProgressDialogWidget progressDialogWidget;
	private HashMap<Integer, String> Projects = new HashMap<Integer, String>();
	private HashMap<Integer, String> ProjetIds = new HashMap<Integer, String>();
	AddPlanAdapter adapter;
	ProjectResultProtocol projectResultProtocol;
	PlanBizImpl planBizImpl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.noTitle();
		Cache.add(this);

		setContentView(R.layout.mecool_plan_add);
		init();
		initToolbar();
		progressDialogWidget = new ProgressDialogWidget(this);
		progressDialogWidget.setDismissTime(30);
		// 关闭输入法
		this.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

		initSQL();

		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				// 成功
				case 1:
						initSQL();
					if (progressDialogWidget.isShowing())
						progressDialogWidget.dismiss();
					System.out.println(ProjetIds.size());
					service();
					adapter.notifyDataSetChanged();
					break;
				// 失败
				case 2:
					if (progressDialogWidget.isShowing())
						progressDialogWidget.dismiss(getCurrentMesg()
								.getContent());
					break;
				}
			}
		};
		PlanSend.getTask(getCurrentUser().getLoginName());
	}

	private void initSQL() {
		ProjectServerBizImpl projectServerBizImpl = new ProjectServerBizImpl(
				this);
		List<ProjectServer> projectServerList = projectServerBizImpl
				.findByProject(0);
		if (projectServerList != null) {
			if (projectServerList.size() > 0) {
				int i = 0;
				for (ProjectServer ps : projectServerList) {
					Projects.put(i, ps.getName());
					ProjetIds.put(i, ps.getProjectId());
					i++;
				}
				service();
			}
		}
	}

	private void init() {
		addPlanListView = (ListView) findViewById(R.id.mecool_plan_add_list);
		autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.mecool_plan_add_autocompletetextview);
		planBizImpl = new PlanBizImpl(this);
	}

	private void service() {
		if (adapter == null) {
			adapter = new AddPlanAdapter(MakePlanSelectProjectActivity.this,
					Projects);
			addPlanListView.setAdapter(adapter);
			addPlanListView.setOnItemClickListener(addPlanListener);
		} else
			adapter.notifyDataSetChanged();
	}

	public OnItemClickListener addPlanListener = new ListView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("ProjectName", Projects.get(arg2));
			bundle.putString("ProjectId", ProjetIds.get(arg2));
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finish();
		}
	};

	private void initToolbar() {

		Toolbar toolbar = new Toolbar(this);
		toolbar.setToolbarCentreText("制定计划-选择项目");
		Toolbar.left_btn.setOnClickListener(onClickListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}

	private OnClickListener onClickListener = new ImageButton.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.mecool_toolbar_leftbtn:
				finish();
				break;
			case R.id.mecool_toolbar_rightbtn:

				break;
			}
		}
	};
}
